﻿using ManhattanProcedure.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ManhattanProcedure
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static bool isMake = true;
        public static double MakingTypes = 0;
        public static string img_path = "";
        public static string IPorPortUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"IPorPortMessage.txt";
        /// <summary>
        /// M12警告
        /// </summary>
        public static string WarnPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Warn";
        /// <summary>
        /// M12故障
        /// </summary>
        public static string BreakPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"BreakDowm";
        public static bool IsStartMakingL = false;
        public static bool IsStartMakingR = false;
        public static int coffeeNameCode = 5001;
        public static string Img = "";
        public static int TypeL = -1;
        public static int sugarCode = 0;
        public static int cupCode = 0;
        public static int BeanCode = 1;
        public static int zsInit = 0;
        public static string EmailError()
        {
            try
            {
                string msg = "<style>table{ border: 1px solid black; border-collapse: collapse; }table td{border: 1px solid #000000; padding: 5px 15px;font-size:14px;}</style><table>";
                if (WuLiaoCoffeeSel.Instance.CoffeeBean_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>CoffeeBean</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>CoffeeBean</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (!WuLiaoCoffeeSel.Instance.Milk_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>Milk</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Milk</td><td>adequate materials</td></tr>";
                if (WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>Cup1</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Cup1</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                msg += "</table>";
                return msg;
            }
            catch (Exception)
            {
                return "";
            }
        }
        public MainWindow()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 全部订单查询
        /// </summary>
        public static void MessageShow()
        {
            while (true)
            {
                try
                {
                    Device device = new Device();
                    string lists = JsonConvert.SerializeObject(device);
                    var data = HttpMsg.Instance.deviceOrderQueueListMsg(lists);

                    if (data != null)
                    {
                        if (isMake)
                        {
                            var msglist = JsonConvert.DeserializeObject<HTTPMsgs>(data);
                            if (msglist != null)
                            {
                                if (msglist.data.ToString().Length > 10)
                                {
                                    var list = JsonConvert.DeserializeObject<List<deviceOrderQueueList>>(msglist.data.ToString());
                                    if (list.Count > 0)
                                    {
                                        if (isMake)
                                        {
                                            zsInit = 0;
                                            if (list.Count > 0)
                                            {
                                                var ddd = list.FirstOrDefault(a => a.typeList2.Count > 0);
                                                if (ddd == null) continue;
                                                if (ddd.typeList2.Count > 0)
                                                {
                                                    var make = ddd.typeList2.FirstOrDefault(a => a.status == "3" || a.status == "4");
                                                    if (make != null)
                                                    {
                                                        if (make.jsonCodeVal.Length > 4)
                                                        {
                                                            var coffeecode = JsonConvert.DeserializeObject<List<Specification>>(make.jsonCodeVal);
                                                            if (coffeecode.Count > 0)
                                                            {
                                                                var coffeeID = coffeecode.FirstOrDefault(a => a.classCode != "");
                                                                if (coffeeID != null)
                                                                {
                                                                    RobotOperate.CX_Start();
                                                                    Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                                                                    if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                                                                    {
                                                                        if (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)
                                                                            CoffeeMaking(coffeeID.classCode, make, ddd.orderNum);
                                                                        //  CoffeeMaking("5203", null, "5003");
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    //zsInit++;
                                    //if (zsInit == 10)
                                    //{
                                    //    RobotOperate.CX_Start();
                                    //    Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                                    //    if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                                    //    {
                                    //        IOMsg.Instance.AllBol = false;
                                    //        Thread.Sleep(200);
                                    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_ZS_UP);
                                    //        Thread.Sleep(200);
                                    //        while (true)
                                    //        {
                                    //            IOMsg.Instance.RobotBol = false;
                                    //            Thread.Sleep(200);
                                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_ZSWC);
                                    //            Thread.Sleep(200);
                                    //            if (IOMsg.Instance.RobotBol)
                                    //            {
                                    //                Thread.Sleep(200);
                                    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_ZS_Down);
                                    //                Thread.Sleep(200);
                                    //                break;
                                    //            }
                                    //        }
                                    //        Thread.Sleep(500);
                                    //        IOMsg.Instance.AllBol = true;
                                    //    }
                                    //    isMake = true;
                                    //    zsInit = 0;
                                    //}
                                }
                            }

                        }
                    }
                    Thread.Sleep(1000);
                }
                catch (Exception)
                {
                    //return;
                    Thread.Sleep(1000);
                }
            }

        }
    }
}
